package day_2022_4_to_7.day48;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @author haomin
 * @date 2022/05/14 14:09
 **/
// ?*Bc*?
// abcd
public class Day18_2_String {
    public static void main2(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String regex = sc.next().toLowerCase();
            String str = sc.next().toLowerCase();
            // * 匹配0个或以上的数字英文字符 (注意：需要考虑连续出现多个*的情况)
            regex = regex.replaceAll("\\*{1,}", "[0-9A-Za-z]*");
            // ? 匹配1个数字英文字符
            regex = regex.replaceAll("\\?", "[0-0A-Za-z]{1}");
            System.out.println(str.matches(regex));
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String pattern;
        while ((pattern = br.readLine()) != null) {
            pattern = pattern.trim();
            String str = br.readLine().trim();
            System.out.println(isMatch(pattern, str));
        }
    }

    private static boolean isMatch(String p, String s) {
        int plen = p.length();
        int slen = s.length();
        boolean[][] dp = new boolean[slen + 1][plen + 1];
        // 字符串和模式串均为空，直接匹配上
        dp[0][0] = true;
        // 模式串为空，全都不匹配，j=0时全部为false
        // i=0时，只有在模式串为*的时候才能匹配
        for (int j = 1; j <= plen; j++) {
            if (p.charAt(j - 1) == '*')
                dp[0][j] = true;
            else
                break;
        }
        for (int i = 1; i <= slen; i++) {
            for (int j = 1; j <= plen; j++) {
                if (p.charAt(j - 1) == '*')
                    dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                else if (p.charAt(j - 1) == '?' || p.charAt(j - 1) == s.charAt(i - 1))
                    dp[i][j] = dp[i - 1][j - 1];
            }
        }
        return dp[slen][plen];
    }
}